package com.pc.scrs.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pc.scrs.bean.po.Course;
import com.pc.scrs.bean.po.Student;
import com.pc.scrs.bean.po.Users;
import com.pc.scrs.bean.vo.listVo.MySelectListVo;
import com.pc.scrs.bean.vo.listVo.RequiredListVo;
import com.pc.scrs.bean.vo.listVo.SelectListVo;
import com.pc.scrs.bean.vo.pageInfo.MySelectListVoPageInfo;
import com.pc.scrs.bean.vo.pageInfo.RecommendListVoPageInfo;
import com.pc.scrs.bean.vo.pageInfo.RequiredListVoPageInfo;
import com.pc.scrs.bean.vo.pageInfo.SelectListVoPageInfo;
import com.pc.scrs.dao.CourseDao;
import com.pc.scrs.dao.StudentDao;
import com.pc.scrs.dao.UsersDao;
import com.pc.scrs.service.ISelectService;
import com.pc.scrs.utils.JwtUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class SelectService implements ISelectService {

    @Resource
    private CourseDao courseDao;

    @Resource
    private UsersDao usersDao;

    @Resource
    private StudentDao studentDao;

    @Override
    public RequiredListVoPageInfo requiredList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Course> requiredList = courseDao.getRequiredList();
        List<RequiredListVo> requiredListVoList = new ArrayList<>();
        PageInfo<Course> coursePageInfo = new PageInfo<>(requiredList);
        for (Course course : requiredList) {
            RequiredListVo requiredListVo = new RequiredListVo();
            requiredListVo.setId(course.getId());
            requiredListVo.setCover(course.getCover());
            requiredListVo.setTitle(course.getTitle());
            requiredListVo.setLessonNum(course.getLessonNum());
            requiredListVo.setSubjectName(course.getSubject().getTitle());
            requiredListVoList.add(requiredListVo);
        }
        RequiredListVoPageInfo requiredListVoPageInfo = new RequiredListVoPageInfo();
        requiredListVoPageInfo.setRequiredListVoList(requiredListVoList);
        requiredListVoPageInfo.setPages(coursePageInfo.getPages());
        requiredListVoPageInfo.setPageNum(coursePageInfo.getPageNum());
        requiredListVoPageInfo.setPageSize(coursePageInfo.getPageSize());
        requiredListVoPageInfo.setTotal(coursePageInfo.getTotal());
        return requiredListVoPageInfo;
    }

    @Override
    public SelectListVoPageInfo selectList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Course> selectList = courseDao.getSelectList();
        List<SelectListVo> selectListVoList = new ArrayList<>();
        PageInfo<Course> coursePageInfo = new PageInfo<>(selectList);
        for (Course course : selectList) {
            SelectListVo selectListVo = new SelectListVo();
            selectListVo.setId(course.getId());
            selectListVo.setCover(course.getCover());
            selectListVo.setTitle(course.getTitle());
            selectListVo.setLessonNum(course.getLessonNum());
            selectListVo.setSubjectName(course.getSubject().getTitle());
            selectListVoList.add(selectListVo);
        }
        SelectListVoPageInfo selectListVoPageInfo = new SelectListVoPageInfo();
        selectListVoPageInfo.setSelectListVoList(selectListVoList);
        selectListVoPageInfo.setPageNum(coursePageInfo.getPageNum());
        selectListVoPageInfo.setPageSize(coursePageInfo.getPageSize());
        selectListVoPageInfo.setPages(coursePageInfo.getPages());
        selectListVoPageInfo.setTotal(coursePageInfo.getTotal());
        return selectListVoPageInfo;
    }

    @Override
    public Integer selectCourse(String token, Integer id) {
        //根据controller层获取token并解析出userid
        String username = JwtUtils.getUserNameFormJwt(token);
        Users users = usersDao.userInfo(username);
        Student student = studentDao.queryStudentByUserId(users.getId());
        Course course = courseDao.selectedOrNot(student.getId(), id);
        if(course!=null){
            return null;
        }else {
            Integer result = courseDao.selectCourse(student.getId(), id);
            return result;
        }
    }

    @Override
    public MySelectListVoPageInfo mySelectList(String token, Integer pageNum, Integer pageSize) {
        //根据controller层获取token并解析出userid
        String username = JwtUtils.getUserNameFormJwt(token);
        Users users = usersDao.userInfo(username);
        Student student = studentDao.queryStudentByUserId(users.getId());
        PageHelper.startPage(pageNum, pageSize);
        List<Course> mySelectList = courseDao.mySelectList(student.getId());
        List<MySelectListVo> mySelectListVoList = new ArrayList<>();
        PageInfo<Course> coursePageInfo = new PageInfo<>(mySelectList);
        for (Course course : mySelectList) {
            MySelectListVo mySelectListVo = new MySelectListVo();
            mySelectListVo.setId(course.getId());
            mySelectListVo.setTitle(course.getTitle());
            mySelectListVo.setCover(course.getCover());
            mySelectListVo.setLessonNum(course.getLessonNum());
            mySelectListVo.setSubjectName(course.getSubject().getTitle());
            mySelectListVo.setRequired(course.getRequired());
            mySelectListVo.setViewCount(course.getViewCount());
            mySelectListVo.setTeacherName(course.getTeacher().getName());
            mySelectListVoList.add(mySelectListVo);
        }
        MySelectListVoPageInfo mySelectListVoPageInfo = new MySelectListVoPageInfo();
        mySelectListVoPageInfo.setMySelectListVoList(mySelectListVoList);
        mySelectListVoPageInfo.setPageNum(coursePageInfo.getPageNum());
        mySelectListVoPageInfo.setPageSize(coursePageInfo.getPageSize());
        mySelectListVoPageInfo.setPages(coursePageInfo.getPages());
        mySelectListVoPageInfo.setTotal(coursePageInfo.getTotal());
        return mySelectListVoPageInfo;
    }

    @Override
    public Integer cancelCourse(String token, Integer id) {
        //根据controller层获取token并解析出userid
        String username = JwtUtils.getUserNameFormJwt(token);
        Users users = usersDao.userInfo(username);
        Student student = studentDao.queryStudentByUserId(users.getId());
        Integer result = courseDao.cancelCourse(student.getId(), id);
        return result;
    }


}
